﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Infragistics.Windows.DataPresenter;
using QLKS.Models;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmLanguageManager.xaml
    /// </summary>
    public partial class frmLanguageManager : Window
    {
        private string CellValue = string.Empty;
        private string CellField = string.Empty;
        private DataSet ds = new DataSet();
        private string FileName = string.Empty;

        public frmLanguageManager()
        {
            InitializeComponent();
        }
        public frmLanguageManager(DataSet ds,string filename)
        {
            InitializeComponent();
            SysLib.SysObj.Waiting(100, 50);
            this.ds = ds;
            FileName = filename;
            xdgResult.DataSource = this.ds.Tables[0].DefaultView;
            SysLib.SysObj.Waiting(100, 99);
        }

        private void xdgResult_EditModeEnded(object sender, Infragistics.Windows.DataPresenter.Events.EditModeEndedEventArgs e)
        {
            try
            {
                string NewValue = xdgResult.ActiveCell.Value.ToString();
                if (NewValue.Equals(CellValue))
                    return;
                //DataRowView row = (xdgResult.ActiveRecord as DataRecord).DataItem as DataRowView;
                
                //DataLib.RunExecute("update controls set " + CellField + " = N'" + row[CellField].ToString() + "' where form like '" + row["form"].ToString() + "' and [control] like '" + row["control"].ToString() + "'");
            }
            catch
            { if (DataProviderModel.conn.State == ConnectionState.Open) DataProviderModel.conn.Close(); }
        }

        private void xdgResult_CellActivated(object sender, Infragistics.Windows.DataPresenter.Events.CellActivatedEventArgs e)
        {
            try
            {
                CellValue = xdgResult.ActiveCell.Value.ToString();
                CellField = xdgResult.ActiveCell.Field.Name;
            }
            catch { }
        }

        private void FormTrans_Loaded(object sender, RoutedEventArgs e)
        {
            SysLib.SysObj.Waiting(100, 100);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //XmlProvider.SaveDataTableToXml(tb, FileName);
            ds.WriteXml(FileName,XmlWriteMode.WriteSchema);
        }

        private void xdgResult_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F9)
            {
                if (xdgResult.ActiveRecord != null)
                {
                    int index = xdgResult.ActiveRecord.Index;
                    string text = ds.Tables[0].Rows[index]["vcaption"].ToString();
                    ds.Tables[0].Rows[index]["ecaption"] = Sm.Windows.Controls.ControlLib.ControlFunction.TranslateText(text, "vi|en", System.Text.Encoding.UTF8);
                }
            }
        }
    }
}
